package com.itistimetothink.cjetso;

import java.util.List;

import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.google.android.maps.MapView;
import com.itistimetothink.cjetso.utilities.ARData;
import com.itistimetothink.cjetso.utilities.Marker;
import com.itistimetothink.networkdata.NetworkDataSource;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

public class AugmentedMain extends AugmentedActivity {
	public static String category, type;
	Button tab1, tab2, tab3, tab4, tab5;
	protected static LinearLayout tabsLayout = null;
	private static int width, height;

	
	private static final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(
			1);
	private static final ThreadPoolExecutor exeService = new ThreadPoolExecutor(
			1, 1, 20, TimeUnit.SECONDS, queue);
	private static final Map<String, NetworkDataSource> sources = new ConcurrentHashMap<String, NetworkDataSource>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = this.getIntent().getExtras();
		category = bundle.getString("category");
        Log.i("AugmentedMain", "AugmentedMain onCreate");
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		width = displaymetrics.widthPixels;
		height = displaymetrics.heightPixels;
		
        boolean downloadSuccessfully = bundle.getBoolean("downloadSuccessfully");
        if(!downloadSuccessfully){
        	Toast.makeText(this, "Nothing Around!!", Toast.LENGTH_SHORT).show();
        }
        tabsLayout = new LinearLayout(this);
		tabsLayout.setOrientation(LinearLayout.VERTICAL);
		tabsLayout.setPadding(5, 5, 5, 5);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		if (category.equalsIgnoreCase("food")) {
			tab1 = new Button(this);
			tab2 = new Button(this);
			tab3 = new Button(this);
			tab5 = new Button(this);
	        Log.i("AugmentedMain", "AugmentedMain food");
			tab1.setBackgroundResource(R.drawable.restaurant_pressed);
			tab1.setTag("restaurant");
			tab1.setOnClickListener(listener);
			tab1.setHeight(height/5);
			tab1.setWidth(height/5);
			
			tab2.setBackgroundResource(R.drawable.cafe_and_club);
			tab2.setTag("cafe_and_club");			
			tab2.setOnClickListener(listener);
			tab2.setHeight(height/5);
			tab2.setWidth(height/5);
			
			tab3.setBackgroundResource(R.drawable.snack);
			tab3.setTag("snack");		
			tab3.setOnClickListener(listener);
			tab3.setHeight(height/5);
			tab3.setWidth(height/5);
			
			tab5.setBackgroundResource(R.drawable.jetso);
			tab5.setTag("jetso");			
			tab5.setOnClickListener(listener);
			tab5.setHeight(height/5);
			tab5.setWidth(height/5);
			ARData.setType("restaurant");

			tabsLayout.addView(tab1, layoutParams);
			tabsLayout.addView(tab2, layoutParams);
			tabsLayout.addView(tab3, layoutParams);
			tabsLayout.addView(tab5, layoutParams);
		} else if (category.equalsIgnoreCase("shop")) {
	        Log.i("AugmentedMain", "AugmentedMain shop");
			tab1 = new Button(this);
			tab2 = new Button(this);
			tab3 = new Button(this);
			tab4 = new Button(this);
			tab5 = new Button(this);

			tab1.setBackgroundResource(R.drawable.fashion_pressed);
			tab1.setTag("fashion");
			tab1.setOnClickListener(listener);
			tab1.setHeight(height/5);
			tab1.setWidth(height/5);
			
			tab2.setBackgroundResource(R.drawable.electronic);
			tab2.setTag("electronic");
			tab2.setOnClickListener(listener);
			tab2.setHeight(height/5);
			tab2.setWidth(height/5);
			
			tab3.setBackgroundResource(R.drawable.beauty);
			tab3.setTag("beauty");
			tab3.setOnClickListener(listener);
			tab3.setHeight(height/5);
			tab3.setWidth(height/5);
			
			tab4.setBackgroundResource(R.drawable.health);
			tab4.setTag("health");
			tab4.setOnClickListener(listener);
			tab4.setHeight(height/5);
			tab4.setWidth(height/5);
			
			tab5.setBackgroundResource(R.drawable.jetso);
			tab5.setTag("jetso");
			tab5.setOnClickListener(listener);
			tab5.setHeight(height/5);
			tab5.setWidth(height/5);
			ARData.setType("fashion");


			tabsLayout.addView(tab1, layoutParams);
			tabsLayout.addView(tab2, layoutParams);
			tabsLayout.addView(tab3, layoutParams);
			tabsLayout.addView(tab4, layoutParams);
			tabsLayout.addView(tab5, layoutParams);
		} else if (category.equalsIgnoreCase("leisure")) {
	        Log.i("AugmentedMain", "AugmentedMain leisure");
			tab1 = new Button(this);
			tab2 = new Button(this);
			tab3 = new Button(this);
			tab4 = new Button(this);
			tab5 = new Button(this);
			tab1.setBackgroundResource(R.drawable.book_pressed);
			tab1.setTag("book");
			tab1.setOnClickListener(listener);
			tab1.setHeight(height/5);
			tab1.setWidth(height/5);
			tab2.setBackgroundResource(R.drawable.accessory);
			tab2.setTag("accessory");
			tab2.setOnClickListener(listener);
			tab2.setHeight(height/5);
			tab2.setWidth(height/5);
			tab3.setBackgroundResource(R.drawable.toy);
			tab3.setTag("toy");
			tab3.setOnClickListener(listener);
			tab3.setHeight(height/5);
			tab3.setWidth(height/5);
			tab4.setBackgroundResource(R.drawable.event);
			tab4.setTag("event");
			tab4.setOnClickListener(listener);
			tab4.setHeight(height/5);
			tab4.setWidth(height/5);
			tab5.setBackgroundResource(R.drawable.jetso);
			tab5.setTag("jetso");			 
			tab5.setOnClickListener(listener);
			tab5.setHeight(height/5);
			tab5.setWidth(height/5);
			ARData.setType("book");

			tabsLayout.addView(tab1, layoutParams);
			tabsLayout.addView(tab2, layoutParams);
			tabsLayout.addView(tab3, layoutParams);
			tabsLayout.addView(tab4, layoutParams);
			tabsLayout.addView(tab5, layoutParams);
		}else{
	        Log.i("AugmentedMain", "AugmentedMain Bundle error");
		}
        FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(  LayoutParams.WRAP_CONTENT, 
                LayoutParams.FILL_PARENT, 
                Gravity.RIGHT);
        addContentView(tabsLayout,frameLayoutParams);

	}
	Button.OnClickListener listener = new Button.OnClickListener() {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String getType = (String) v.getTag();
			if(getType.equalsIgnoreCase("restaurant")){
				ScreenInit(); 
				tab1.setBackgroundResource(R.drawable.restaurant_pressed);			
			}else if(getType.equalsIgnoreCase("cafe_and_club")){
				ScreenInit();
				tab2.setBackgroundResource(R.drawable.cafe_and_club_pressed);			
			}else if(getType.equalsIgnoreCase("snack")){
				ScreenInit();
				tab3.setBackgroundResource(R.drawable.snack_pressed);		
			}else if(getType.equalsIgnoreCase("fashion")){
				ScreenInit();
				tab1.setBackgroundResource(R.drawable.fashion_pressed);		
			}else if(getType.equalsIgnoreCase("electronic")){
				ScreenInit();
				tab2.setBackgroundResource(R.drawable.electronic_pressed);	
			}else if(getType.equalsIgnoreCase("beauty")){
				ScreenInit();
				tab3.setBackgroundResource(R.drawable.beauty_pressed);	
			}else if(getType.equalsIgnoreCase("health")){
				ScreenInit();
				tab4.setBackgroundResource(R.drawable.health_pressed);	
			}else if(getType.equalsIgnoreCase("book")){
				ScreenInit();
				tab1.setBackgroundResource(R.drawable.book_pressed);	
			}else if(getType.equalsIgnoreCase("accessory")){
				ScreenInit();
				tab2.setBackgroundResource(R.drawable.accessory_pressed);	
			}else if(getType.equalsIgnoreCase("toy")){
				ScreenInit();
				tab3.setBackgroundResource(R.drawable.toy_pressed);	
			}else if(getType.equalsIgnoreCase("event")){
				ScreenInit();
				tab4.setBackgroundResource(R.drawable.event_pressed);	
			}else if(getType.equalsIgnoreCase("jetso")){
				ScreenInit();
				tab5.setBackgroundResource(R.drawable.jetso_pressed);	
			}
			type = getType;
			ARData.setType(type);	
            camScreen.invalidate();
		}
	};
	
	@Override
	public void onLocationChanged(Location location) {
		super.onLocationChanged(location);
		updateData(location.getLatitude(), location.getLongitude(),
				location.getAltitude());
	}
	
	private void updateData(final double lat, final double lon, final double alt) {

		try {
			exeService.execute(new Runnable() {

				public void run() {
					for (NetworkDataSource source : sources.values())
						download(source, lat, lon);
				}
			});
		} catch (RejectedExecutionException rej) {

		} catch (Exception e) {

		}

	}

	private static boolean download(NetworkDataSource source, double lat,
			double lon) {
		if (source == null)
			return false;

		String url = null;
		try {
			//changed
			url = source.createRequestURL(lat, lon);
		} catch (NullPointerException e) {
			return false;
		}

		List<Marker> markers = null;
		try {
			markers = source.parse(url);
		} catch (NullPointerException e) {
			return false;
		}

		ARData.addMarkers(markers);
		return true;
	}
	
	@Override
	protected void markerTouched(Marker marker) {
        Toast t = Toast.makeText(getApplicationContext(), marker.getName(), Toast.LENGTH_SHORT);
        t.setGravity(Gravity.CENTER, 0, 0);
        t.show();
        String title, phone, address, description, coupon;       
        String pic_1, pic_2, pic_3, coupon_pic;
        int like_count, dislike_count;
        double lat, lng;
        title = marker.getName();
        phone = marker.getPhone();
        //if(phone.equalsIgnoreCase("null")||phone==null){
        //	phone = getString(R.string.no_phone);
        //}
        address = marker.getAdress();
        like_count = marker.getLike();
        dislike_count = marker.getDislike();
        lat = marker.getLat();
        lng = marker.getLng();
        description = marker.getDescription();
        //if(description.equalsIgnoreCase("null")||description==null){
        //	description = getString(R.string.no_description);
        //}
        coupon = marker.getCouponString();
        //if(coupon.equalsIgnoreCase("null")||coupon==null){
        //	coupon = getString(R.string.no_coupon);
        //}
        pic_1 = marker.getPicture1();        
        pic_2 = marker.getPicture2();
        pic_3 = marker.getPicture3();
        coupon_pic = marker.getCoupon_pic();
        int not_here = 0;
        not_here = marker.getNothere();
        Bundle bundle = new Bundle();
		bundle.putString("title", title);
		bundle.putString("phone", phone);
		bundle.putString("address", address);
		bundle.putString("description", description);
		bundle.putInt("like_count", like_count);
		bundle.putInt("dislike_count", dislike_count);
		bundle.putInt("not_here", not_here);
		bundle.putDouble("lat", lat);
		bundle.putDouble("lng", lng);
		bundle.putString("coupon", coupon);
		bundle.putString("pic_1", pic_1);
		bundle.putString("pic_2", pic_2);
		bundle.putString("pic_3", pic_3);
		bundle.putString("coupon_pic", coupon_pic);
		bundle.putString("category", category);
		Intent myIntent = new Intent(this, ShopInfo.class);
		myIntent.putExtras(bundle);
		startActivity(myIntent);		
	}
	
	public void ScreenInit(){
		if (category.equalsIgnoreCase("food")) {
			
			tab1.setBackgroundResource(R.drawable.restaurant);
			tab2.setBackgroundResource(R.drawable.cafe_and_club);			
			tab3.setBackgroundResource(R.drawable.snack);			
			tab5.setBackgroundResource(R.drawable.jetso);

		} else if (category.equalsIgnoreCase("shop")) {
			tab1.setBackgroundResource(R.drawable.fashion);
			tab2.setBackgroundResource(R.drawable.electronic);
			tab3.setBackgroundResource(R.drawable.beauty);
			tab4.setBackgroundResource(R.drawable.health);
			tab5.setBackgroundResource(R.drawable.jetso);

		} else if (category.equalsIgnoreCase("leisure")) {
	        Log.i("AugmentedMain", "AugmentedMain leisure");
			tab1.setBackgroundResource(R.drawable.book);
			tab2.setBackgroundResource(R.drawable.accessory);			
			tab3.setBackgroundResource(R.drawable.toy);
			tab4.setBackgroundResource(R.drawable.event);
			tab5.setBackgroundResource(R.drawable.jetso);

		}
	}
}
